import { RequireJs, GetText } from './request'

/* iconfont: https://www.iconfont.cn/ => 我的项目 => Symbol => 复制JS链接 */
const iconfontUrl = '//at.alicdn.com/t/c/font_2475315_kjj3u5juio.js'
RequireJs(iconfontUrl)


// 获取项目的iconfont的图标
export const GetIconfontArr = (val?: string) => {
  return new Promise((resolve, reject) => {
    return GetText(iconfontUrl).then((data: any) => {
      const txt: any = data
      if (!txt || txt === '') return
      const iconArr = txt.match(/id="(\S*)/gm)
      let icons: any[] = []
      iconArr.forEach((v: string) => {
        icons.push(v.split('=')[1].replace('"', '').replace('"', '').replace('icon', ''))
      })
      if (val) {
        icons = icons.filter(item => {
          return item.indexOf(val) == 0
        })
      }
      resolve(icons)
    }).catch(() => {
      reject('getIcons错误')
    })
  })
}